/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaclientcp3110.UCM;
import common.UCM.UCController;
import javaclientcp3110.Model.Component.CompAddModel;
import common.Objects.Component;
import javaclientcp3110.Model.MainModel;
import javaclientcp3110.View.Component.CompAddView;

/**
 * UC Add Component
 *
 * Pre-condition: A valid user is logged into the system.
 *
 *  1. User selects to add a component.
 *  2. System displays the add component view.
 *  3. User enters the component details and clicks save
 *  4. System gets component details from view
 *  5. System adds the component to the storage medium
 *  6. System displays a success message.
 *
 * Post-condition: The new component is added to the system
 *
 * Unsuccessful Outcomes:
 *
 *   * 3a) User enters invalid data.
 *        1. System displays an error message
 *        2. System prompts user to try again
 *   * 5a) System is unable to update the storage medium
 *        1. System displays an error message
 * @author Richard Irons
 */
public class UCAddComponent implements UCController{
    private CompAddView view;
    private CompAddModel model;
    /**
     * Defualt constructor, creates new model and view
     */
    public UCAddComponent() {
        this (new CompAddView(), new CompAddModel());
    }
    /**
     * Constructor that sets the model and view to the inputs
     * @param view - view to set to
     * @param model - model to set to
     */
    public UCAddComponent(CompAddView view, CompAddModel model) {
        this.view = view;
        this.model = model;
    }
    /**
     * Implement uccontrollers run function. This is the body of the use case, it
     * is where the use case is run.
     */
    public void run() {
        boolean itemAdded = false;
        boolean itemValid = false;
    //*  1. User selects to add a component.
    //*  2. System displays the add component view.
    //*  3. User enters the component details and clicks save
    
    //* Unsuccessful Outcomes:
    //*   * 3a) User enters invalid data.
    //*        1. System displays an error message
    //*        2. System prompts user to try again
        itemValid = validateComponent();
        if (itemValid == false) {
            view.errorMessage("The entered component details are not valid " +
                    "\n Please try again.");
        }
        else {
    //*  4. System gets component details from view
            Component tempComponent = new Component();
            
            tempComponent.setName(view.getCompName());
            tempComponent.setDescription(view.getCompDescription());
            tempComponent.setCurrentStock(view.getCompCurrentStock());
            tempComponent.setPrice(view.getCompPrice());
            tempComponent.setCategoryId(view.getCompCategory().getId());

            model.setNewComponent(tempComponent);
            
    //*  5. System adds the component to the storage medium
            itemAdded = model.addNewComponent();
    //*  6. System displays a success message.
            if (itemAdded) {
                view.successMessage("Component successfully added!");
                MainModel.getInstance().updateData();
                view.clearFields();
            }
            else {
    //* Unsuccessful Outcomes:
    //*   * 5a) System is unable to update the storage medium
    //*        1. System displays an error message
                view.errorMessage("Component could not be added");
            }
        }
    }

    private boolean validateComponent() {
        if (view.getCompName().equals(null)) {
            return false;
        }
        if (view.getCompDescription().equals(null)) {
            return false;
        }
        try {
            view.getCompCurrentStock();
        }
        catch (NumberFormatException e) {
            return false;
        }
        try {
            view.getCompPrice();
        }
        catch (NumberFormatException e) {
            return false;
        }
        return true;

    }

}
